<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="cn.estore.util.DBConnection" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>用户信息修改</title>
	<style>
		body {
			font-family: Arial, sans-serif;
			background-color: #f8f9fa;
			margin: 0;
			padding: 0;
			text-align: center;
		}

		header {
			background-color: #343a40;
			color: white;
			padding: 20px 0;
		}

		h1 {
			margin: 0;
		}

		main {
			padding: 20px;
		}

		table {
			width: 80%;
			margin: 20px auto;
			border-collapse: collapse;
			box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
		}

		th, td {
			padding: 12px 15px;
			text-align: left;
			border-bottom: 1px solid #ddd;
		}

		th {
			background-color: #495057;
			color: white;
		}

		tr:hover {
			background-color: #e9ecef;
		}

		input[type="text"] {
			width: 100%;
			padding: 8px;
			border: 1px solid #ccc;
			border-radius: 4px;
		}

		input[type="submit"] {
			background-color: #4CAF50;
			color: white;
			padding: 10px 20px;
			border: none;
			border-radius: 4px;
			cursor: pointer;
		}

		input[type="submit"]:hover {
			background-color: #45a049;
		}
	</style>
</head>
<body>
<header>
	<h1>用户信息修改</h1>
</header>
<main>
	<%
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");

		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			// 获取数据库连接
			DBConnection jdbc = new DBConnection();
			conn = jdbc.getConnection();

			// 使用PreparedStatement构建查询语句，防止SQL注入
			String sql = "select * from user where username =?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, username);

			rs = stmt.executeQuery();

			if (rs.next()) {
				int id = rs.getInt(1);
				String realname = rs.getString(3);
				int age = rs.getInt(4);
				String sex = rs.getString(5);
				String pwd = rs.getString(6);
				String zhuanye = rs.getString(7);
				String aihao = rs.getString(8);
				String email = rs.getString(9);
	%>
	<form action="UpdateUserByUnamego1.jsp" method="post">
		<table border="1" align="center">
			<tr>
				<td colspan="2">
					<center>您修改的信息是：</center>
				</td>
			</tr>
			<tr>
				<td>序号：</td>
				<td>
					<input type="text" name="id" value="<%=id%>">
				</td>
			</tr>
			<tr>
				<td>用户名：</td>
				<td>
					<input type="text" name="username" value="<%=username%>">
				</td>
			</tr>
			<tr>
				<td>真实姓名：</td>
				<td>
					<input type="text" name="realname" value="<%=realname%>">
				</td>
			</tr>
			<tr>
				<td>年龄：</td>
				<td>
					<input type="text" name="age" value="<%=age%>">
				</td>
			</tr>
			<tr>
				<td>性别：</td>
				<td>
					<input type="text" name="sex" value="<%=sex%>">
				</td>
			</tr>
			<tr>
				<td>密码：</td>
				<td>
					<input type="text" name="pwd" value="<%=pwd%>">
				</td>
			</tr>
			<tr>
				<td>专业：</td>
				<td>
					<input type="text" name="zhuanye" value="<%=zhuanye%>">
				</td>
			</tr>
			<tr>
				<td>爱好：</td>
				<td>
					<input type="text" name="aihao" value="<%=aihao%>">
				</td>
			</tr>
			<tr>
				<td>电子邮箱：</td>
				<td>
					<input type="text" name="email" value="<%=email%>">
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<center>
						<input type="submit" value="确定修改">
					</center>
				</td>
			</tr>
		</table>
	</form>
	<%
			} else {
				out.print("查无此人！");
			}
		} catch (SQLException e) {
			// 捕获数据库操作异常，输出提示信息告知用户
			out.println("数据库查询出现异常，请稍后再试！");
			e.printStackTrace();
		} finally {
			// 使用try-with-resources语句自动关闭资源，无需手动编写关闭代码，增强代码可靠性
			try {
				if (rs!= null) {
					rs.close();
				}
				if (stmt!= null) {
					stmt.close();
				}
				if (conn!= null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	%>
</main>
</body>
</html>
